package com.probox.modules.controller.wechat;

import com.alibaba.fastjson.JSONObject;
import com.probox.common.core.enums.PageTypeEnum;
import com.probox.common.core.utils.SecurityUtils;
import com.probox.common.core.web.controller.BaseController;
import com.probox.common.entity.domain.platfom.vo.ConfigVo;
import com.probox.common.entity.domain.wechat.vo.CodeTemplateVo;
import com.probox.common.entity.service.platfom.IPlatformPageConfigService;
import com.probox.common.entity.service.wechat.WechatBroadcastService;
import com.probox.common.entity.service.wechat.WechatMiniService;
import com.probox.common.entity.service.wechat.WechatPlatformService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;

/**
 * 快速注册小程序
 *
 * @author usoft
 * @date 2021-05-28
 */
@RestController
@RequestMapping("/wechat/applet")
public class WechatAppletIssueController extends BaseController {

    @Autowired
    private WechatMiniService wechatMiniService;

    @Autowired
    private WechatPlatformService wechatPlatformService;

    @Autowired
    private WechatBroadcastService wechatBroadcastService;

    @Autowired
    private IPlatformPageConfigService iPlatformPageConfigService;

    /**
     * 获取代码模板列表
     */
    @PostMapping("/gettemplatelist")
    public CodeTemplateVo gettemplatelist(@RequestBody String authorizationType){
        String templateType = null;
        logger.info("开始获取代码模板列表...");
        CodeTemplateVo templatevo =  wechatMiniService.gettemplatelist(templateType,authorizationType);
        return templatevo;
    }

    /**
     * 上传代码
     */
    public String uploadCode(String templateId,String templateType,String userVersion,String authorizationType,Boolean funcType){
//        String userVersion = "0.0.0";
        String userDesc = "上传代码";
        logger.info("开始上传代码,请求参数为:templateId=>"+templateId+",userVersion=>"+userVersion+"userDesc=>"+userDesc);
        String rst = wechatMiniService.uploadCode(templateId,templateType,userVersion,userDesc,authorizationType,funcType);
        logger.info("上传代码返回值为=>"+rst);
        return rst;
    }

    /**
     * 获取体验版二维码
     */
    @PostMapping("/getQrcode")
    public String getQrcode(@RequestBody String authorizationType)throws IOException{
        logger.info("开始获取体验版二维码...");
        String rst = wechatMiniService.getQrcode(authorizationType);
        logger.info("获取体验版二维码返回值为=>"+rst);
        return rst;
    }

    /**
     * 提交审核
     */
    @PostMapping("/submitAudit")
    public String submitAudit(@RequestBody String auditJson){
        ConfigVo configVo = new ConfigVo();
        configVo.setPlatformUid(SecurityUtils.getPlatformId());
        configVo.setPageType(PageTypeEnum.homePage.getKey());
        boolean b = iPlatformPageConfigService.checkOnLive(configVo);
        logger.info("当前平台是否装修:"+b);
        if(b == false) return "notFitment";
        JSONObject jsonObject = JSONObject.parseObject(auditJson);
        Boolean releaseType =  jsonObject.getBoolean("releaseType");
        String authorizationType =  jsonObject.getString("authorizationType");
        Boolean funcType =  jsonObject.getBoolean("funcType");
        logger.info("开始提交审核:是否自动发布=>"+releaseType+",是否发布直播权限=>"+funcType);
        //获取模板列表,默认选择id最大的模板id
        CodeTemplateVo templateVo = gettemplatelist(authorizationType);
        //修改模板版本号
//        wechatMiniService.updateTemplateVersion(templateVo,authorizationType);
        if (funcType == null){
            funcType = false;
        }
        //判断是否是直播权限
        if (funcType == true){
            String rst = wechatBroadcastService.queryPermissions();
            logger.info("查询是否具有直播权限返回值=>"+rst);
            if (!"0".equals(rst)){
                return rst;
            }
        }
        //上传代码
        String errcode = uploadCode(String.valueOf(templateVo.getTemplate_id()), templateVo.getTemplate_type(),templateVo.getUser_version(),authorizationType,funcType);
        //提交审核
        if ("0".equals(errcode)){
            String rst =  wechatMiniService.submitAudit(templateVo.getUser_version(),releaseType,authorizationType,funcType);
            return rst;
        }else {
            logger.info("上传代码失败");
        }
        return "";
    }

    /**
     * 查询最新一次提交的审核状态
     */
    @PostMapping("/getLatestAuditstatus")
    public JSONObject LatestAuditstatus(@RequestBody String authorizationType){
        //查询最新一次审核状态
        logger.info("开始查询最新一次提交的审核状态...");
        HashMap rstMap = wechatMiniService.getLatestAuditstatus(authorizationType);
        JSONObject auditInfoJson = new JSONObject(rstMap);
        logger.info("查询最新一次提交的审核状态返回值为=>"+auditInfoJson);
        return auditInfoJson;
    }

    /**
     *小程序审核撤回
     */
    @PostMapping("/undocodeaudit")
    public String undocodeaudit(@RequestBody String authorizationType){
        logger.info("开始执行小程序审核撤回...");
        String rst = wechatMiniService.undocodeaudit(authorizationType);
        logger.info("小程序审核撤回返回值为=>"+rst);
        return rst;
    }

    /**
     * 发布已通过审核的小程序
     */
    @PostMapping("/release")
    public String release(@RequestBody String authorizationType){
        logger.info("开始执行发布已通过审核的小程序...");
        String rst = wechatMiniService.release(null,authorizationType);
        logger.info("发布已通过审核的小程序返回值为=>"+rst);
        return rst;
    }

    /**
     * 版本回退
     */
    @PostMapping("/revertcoderelease")
    public String revertcoderelease(@RequestBody String appVersionJson){
        JSONObject jsonObject = JSONObject.parseObject(appVersionJson);
        String appVersion = jsonObject.getString("appVersion");
        String authorizationType = jsonObject.getString("authorizationType");
        logger.info("开始执行版本回退...");
        String rst = wechatMiniService.revertcoderelease(appVersion,authorizationType);
        logger.info("版本回退返回值为=>"+rst);
        return rst;
    }

    /**
     * 设置最低基础库版本
     */
    @PostMapping("/setweappsupportversion")
    public String setweappsupportversion(@RequestBody String versionJson){
        JSONObject jsonObject = JSONObject.parseObject(versionJson);
        String version = jsonObject.getString("version");
        String authorizationType = jsonObject.getString("authorizationType");
        logger.info("开始设置最低基础库版本...");
        String rst = wechatMiniService.setweappsupportversion(version,authorizationType);
        logger.info("设置最低基础库版本返回值为=>"+rst);
        return rst;
    }

    /**
     * 查询服务商的当月提审限额（quota）和加急次数
     */
    @PostMapping("/queryquota")
    public String queryquota(@RequestBody String authorizationType){
        logger.info("开始查询服务商的当月提审限额...");
        String rst = wechatMiniService.queryquota(authorizationType);
        logger.info("查询服务商的当月提审限额返回值为=>"+rst);
        return rst;
    }

    /**
     * 加急审核申请
     * @param
     * @return
     */
    @PostMapping("/speedupaudit")
    public String speedupaudit(@RequestBody String auditJson){
        JSONObject jsonObject = JSONObject.parseObject(auditJson);
        String auditid = jsonObject.getString("auditid");
        String authorizationType = jsonObject.getString("authorizationType");
        logger.info("开始加急审核申请...");
        String rst = wechatMiniService.speedupaudit(auditid,authorizationType);
        logger.info("加急审核申请返回值为=>"+rst);
        return rst;
    }

}
